﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Level10Letters : MonoBehaviour
{
    public List<Text> textList = new List<Text>();
    public Button leftButton;
    public Button rightButton;
    public Button centerButton;
    public Image letterBg;
    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        BtnEvent();
    }

    private void BtnEvent()
    {
        leftButton.onClick.AddListener(() =>
        {
            if (GameCtrl.Instance.curGameState != GameState.Play)
                return;
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            Text firstText = textList[0];
            textList.RemoveAt(0);
            textList.Add(firstText);
            SortTextList();
            JudgeResult();
        });

        rightButton.onClick.AddListener(() =>
        {
            if (GameCtrl.Instance.curGameState != GameState.Play)
                return;
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            Text lastText = textList[textList.Count - 1];
            textList.RemoveAt(textList.Count - 1);
            textList.Insert(0, lastText);
            SortTextList();
            JudgeResult();
        });
        centerButton.onClick.AddListener(() =>
        {
            if (GameCtrl.Instance.curGameState != GameState.Play)
                return;
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            textList[0].gameObject.SetActive(false);
            textList[textList.Count - 1].gameObject.SetActive(false);
            textList.RemoveAt(textList.Count - 1);
            textList.RemoveAt(0);
            letterBg.rectTransform.sizeDelta = new Vector2(60 * textList.Count + 20, 100);
            JudgeResult();
        });
    }

    private void SortTextList()
    {
        int count = textList.Count;

        for (int i = 0; i < count; i++)
        {
            textList[i].transform.SetAsLastSibling();
        }
    }

    private void JudgeResult()
    {
        int count = textList.Count;
        if (count > 4)
            return;
        string tempStr = string.Empty;
        for (int i = 0; i < count; i++)
        {
            tempStr += textList[i].text;
        }
        if (tempStr.Equals(UConst.Str_TargetLove))
            gameLevelItem.TriggerResult(true);
    }

}
